﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TaskLib
{
    class TaskListViewItemComparer : ListViewItemComparer
    {        
        public TaskListViewItemComparer(int column, SortOrder sortOrder) : 
            base(column, sortOrder)
        {
            
        }

        public int Compare(object x, object y)
        {
            int ret = -1;           
         
            var item1 = x as ListViewItem;
            var item2 = y as ListViewItem;
            if (item1 != null && item2 != null)
            {
                string strX = item1.SubItems[column].Text;
                string strY = item2.SubItems[column].Text;

                DateTime dtX, dtY;
                if (DateTime.TryParse(strX, out dtX) && DateTime.TryParse(strY, out dtY))
                {
                    ret = DateTime.Compare(dtX, dtY);
                }
                else
                {
                    ret = String.Compare(strX, strY);
                }

                if (sortOrder == SortOrder.Descending)
                {
                    ret *= -1;
                }
            }
            return ret;
        }       
    }
}
